<!DOCTYPE HTML>
<html>
<link href="main.css" rel="stylesheet" type="text/css">

<title>SoftPlus</title><body>
<table border="0" align="center">
  <tr>
    <td width="150px"></td>
    <td width="1000px"><img src="banner-01n.gif" width="1000" height="90" alt="Softplus"></td>
  </tr>
  <tr>
    <td><?php
      require("navandset.php");
    ?></td>
<td valign="top">

<h1>Add/Edit Customer Details</h1>

<?php
//Connect
$condet = GetGlobalConnectionOptions();
$con = mysql_connect($condet["server"],$condet["username"],$condet["password"]);
if (!$con)
{
	die("Could not connect: " . mysql_error());
}
//Select DB
mysql_select_db($condet["database"], $con);

//Code
//Create the array to store customer details
$cust = array(
	"customer" => "",
	"surname" => "",
	"firstname" => "",
	"phonenumber" => "",
	"streetaddress" => "",
	"suburb" => ""
);

//Create a variable to hold a message that tells the user what has just happened, if something has happened
$retmessage = "";

if($_POST)
{
	//Get input from form
	$cust["customer"] = $_POST["customer"];
	$cust["surname"] = $_POST["surname"];
	$cust["firstname"] = $_POST["firstname"];
	$cust["phonenumber"] = $_POST["phonenumber"];
	$cust["streetaddress"] = $_POST["streetaddress"];
	$cust["suburb"] = $_POST["suburb"];

	//Load customer details from database
	$qry = "SELECT * FROM tblcustomers WHERE customer='".$cust["customer"]."';";
	$dcust = mysql_fetch_array(mysql_query($qry));
	
	switch($_POST["operation"])
	{
		case "Load Customer":
			//If tblCustomers contains a record fot the input email, then load that into the $cust array
			if($dcust)
			{
				$cust = $dcust;
				$retmessage = "Loaded customer details.";
			}
			else
			{
				$retmessage = "No records were found for customer with email ".$cust["customer"].".";
			};
			break;
		case "Save Customer Details":
			//Check if all input fields have been filled
			if(
				$cust["customer"] != "" and
				$cust["surname"] != "" and
				$cust["firstname"] != "" and
				$cust["phonenumber"] != "" and
				$cust["streetaddress"] != "" and
				$cust["suburb"] != "")
			{
				//If tblCustomers contains a record fot the input email, then update it according to the input
				if($dcust)
				{
					$qry = 	"UPDATE tblcustomers ".
						"SET surname='".$cust["surname"].
						"', firstname='".$cust["firstname"].
						"', phonenumber='".$cust["phonenumber"].
						"', streetaddress='".$cust["streetaddress"].
						"', suburb='".$cust["suburb"].
						"' WHERE customer='".$cust["customer"]."';";
					mysql_query($qry);
					if(mysql_error())
					{
						$retmessage = mysql_error();
					}
					else
					{
						$retmessage = "Customer details updated.";
					};
				}
				//If tblCustomers does not contain a record fot the input email, then insert the #cust array details
				else
				{
					$qry = 	"INSERT INTO tblcustomers ".
						"VALUES ('".$cust["customer"].
						"', '".$cust["surname"].
						"', '".$cust["firstname"].
						"', '".$cust["phonenumber"].
						"', '".$cust["streetaddress"].
						"', '".$cust["suburb"]."');";
					mysql_query($qry);
					if(mysql_error())
					{
						$retmessage = mysql_error();
					}
					else
					{
						$retmessage = "New customer created.";
					};
				};
			}
			else
			{
				//If any field is blank, then tell the user that they cannot save yet.
				$retmessage = "Malformed input, please check your input.";
			};
			break;
		case "Delete Customer":
			if($dcust)
			{
				//If the customer record exists, then delete it.
				$qry = 	"DELETE FROM tblcustomers ".
					"WHERE customer='".$cust["customer"]."';";
				mysql_query($qry);
				if(mysql_error())
				{
					$retmessage = mysql_error();
				}
				else
				{
					$retmessage = "Removed customer record."."(".$cust["customer"].")";
				};
			}
			else
			{
				$retmessage = "No records were found for customer with email ".$cust["customer"].".";
			};
			break;
		case "Clear Input Fields":
			//Set all fields to blank
			$cust["customer"] = "";
			$cust["surname"] = "";
			$cust["firstname"] = "";
			$cust["phonenumber"] = "";
			$cust["streetaddress"] = "";
			$cust["suburb"] = "";
			$retmessage = "Cleared input fields";
			break;
	};
};

?>

<form action="frmcust.php" method="post">
<table border="1">
      <tr>
        <td width="150px"><h2>*Customer Email:</h2></td>
        <td><input type="text" name="customer" size="100" maxlength="100" <?php echo "value='".$cust["customer"]."' "; ?> /></td>
        <td><input type="submit" name="operation" value="Load Customer"></td>
      </tr>
      <tr>
        <td><h2>*Surname:</h2></td>
        <td><input type="text" name="surname" size="100" maxlength="100" <?php echo "value='".$cust["surname"]."' "; ?> /></td>
      </tr>
      <tr>
        <td><h2>*Firstname:</h2></td>
        <td><input type="text" name="firstname" size="100" maxlength="100" <?php echo "value='".$cust["firstname"]."' "; ?> /></td>
      </tr>
      <tr>
        <td><h2>*Phone Number:</h2></td>
        <td><input type="text" name="phonenumber" size="100" maxlength="12" <?php echo "value='".$cust["phonenumber"]."' "; ?> /></td>
      </tr>
      <tr>
        <td><h2>*Street Address:</h2></td>
        <td><input type="text" name="streetaddress" size="100" maxlength="200" <?php echo "value='".$cust["streetaddress"]."' "; ?> /></td>
      </tr>
      <tr>
        <td><h2>*Suburb:</h2></td>
        <td><select name="suburb">
	<?php
	$result = mysql_query("SELECT * FROM tblfullsuburbs;");
	$null = true;
	while($row = mysql_fetch_array($result))
	{
		if($cust["suburb"]==$row["suburb"])
		{
			echo "<option value='".$row["suburb"]."' selected='selected'>".$row["suburb"].", ".$row["postcode"].", ".$row["state"]."</option>";
			$null = false;
		}
		else
		{
			echo "<option value='".$row["suburb"]."'>".$row["suburb"].", ".$row["postcode"].", ".$row["state"]."</option>";
		};
	};
	if($null)
	{
		echo "<option value='' selected='selected'>SELECT A SUBURB</option>";
	};
	?>
	</select></td>
      </tr>
</table>
<input type="submit" name="operation" value="Save Customer Details">
<input type="submit" name="operation" value="Delete Customer">
<input type="submit" name="operation" value="Clear Input Fields">
</form>
<h2>Fields marked with a * are required.</h2>
<h2><?php echo $retmessage ?></h2>


<?php
//Disconnect
mysql_close($con);
?>

</td>
  </tr>
</table>
</body>

</html>
